package com.hbzhit.modules.sys.vo;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hbzhit.common.entity.PageQueryEntity;
import com.hbzhit.common.utils.Constant;
import com.hbzhit.common.utils.ShiroUtils;
import com.hbzhit.modules.sys.entity.SysOrgEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

/**
 * @author DING
 */
@Getter
@Setter
@ApiModel("查询参数")
public class SysOrgQueryVO extends PageQueryEntity {

    @ApiModelProperty("编码或名称关键字")
    private String key;

    @ApiModelProperty("组织类型")
    private Integer orgType;

    @ApiModelProperty("上级id")
    private Long pid;

    /**
     * 查询条件
     */
    public LambdaQueryWrapper<SysOrgEntity> queryWrapper() {
        return new LambdaQueryWrapper<SysOrgEntity>()
                .and(StrUtil.isNotBlank(key), wp -> wp
                        .likeRight(SysOrgEntity::getOrgCode, key)
                        .or()
                        .like(SysOrgEntity::getOrgName, key)
                )
                .eq(orgType != null, SysOrgEntity::getOrgType, orgType)
                .eq(pid != null, SysOrgEntity::getPid, pid)
                .likeRight(ObjectUtil.notEqual(Constant.SUPER_ADMIN, ShiroUtils.getUserId()), SysOrgEntity::getOrgCode, ShiroUtils.getUser().getOrgCode())
                .orderByAsc(SysOrgEntity::getOrderNum)
                .orderByAsc(SysOrgEntity::getOrgCode);
    }
}
